from PySide2 import QtCore, QtWidgets

import share
from PySide2.QtWidgets import QHBoxLayout, QPushButton, QWidget, QVBoxLayout, QMessageBox

from elementUI.h5_element import WebEngineView
from util.TableWidgetUtil import TableWidgetUtil
from util.requestUtil import requestUtil


class show_table:
    def __init__(self):
        self.ui = share.SI.main.ui

    def back(self):
        self.ui.stackedWidget.setCurrentIndex(10)

    def findAll(self):
        # 查询分组及单位
        from binding.home_bind import home_bind
        ug = home_bind().get_ug_comboBox_fun()
        name = self.ui.template_select_lineEdit.text() if self.ui.template_select_lineEdit.text() != "" else "None"
        # 拉取模板数据头列表
        dataMap = requestUtil("/template/findShowByGroup/" + ug["group_value"] + "/" + ug["unit_value"] + "/" + name,
                              "GET").dataMap

        for index, item in enumerate(dataMap["data"]):
            item["buttons"] = self.buttonForRow(item["id"], item["name"])

        dataList = dataMap['data']
        # 准备表头数据
        head_list = [{'key': "目标表名", "value": "name"}, {'key': "分组名称", "value": "gname"},
                     {'key': "单位名称", "value": "uname"}, {'key': "操作", "value": "buttons"}]
        TableWidgetUtil().render(self.ui.show_tableWidget, dataList, head_list)
        TableWidgetUtil().head_color_show(self.ui.show_tableWidget)

    # 列表内添加按钮
    def buttonForRow(self, id, name):
        widget = QWidget()

        # 查看
        alterBtn = QPushButton('查看')
        alterBtn.setStyleSheet(''' QPushButton{
                                    text-align : center;
                                    background-color : transparent;
                                    height : 30px;
                                    width:40px;
                                    border-style: outset;
                                    color:#192D9E;
                                    }
                                     
                                    QPushButton:hover{
                                    	color: #192D9E;
                                    	text-decoration:underline;
                                    }''')

        # 删除
        deleteBtn = QPushButton('删除')
        deleteBtn.setStyleSheet(''' QPushButton{
                                    text-align : center;
                                    background-color : transparent;
                                    height : 30px;
                                    width:50px;
                                    border-style: outset;
                                    color:red;}
                                     
                                    QPushButton:hover{
                                    	color: red;
                                    	text-decoration:underline;
                                    }''')

        alterBtn.clicked.connect(lambda: self.alter_fun(str(id), name))
        deleteBtn.clicked.connect(lambda: self.del_fun(str(id)))

        hLayout = QHBoxLayout()
        hLayout.addWidget(alterBtn)
        hLayout.addWidget(deleteBtn)
        hLayout.setContentsMargins(5, 2, 5, 2)
        widget.setLayout(hLayout)
        return widget

        # 查看模板数据

    def alter_fun(self, id, name):

        url = "file:///static/pages/ShowTable.html?token=" + share.token.headers["Authorization"] + "&tid=" + str(
            id) + "&tname=" + name

        try:
            self.ui.webbrowsershow.load(QtCore.QUrl(url))
        except Exception as e:
            vLayout = QVBoxLayout()
            webbrowser = WebEngineView()
            webbrowser.load(QtCore.QUrl(url))
            self.ui.webbrowsershow = webbrowser
            vLayout.addWidget(webbrowser)
            vLayout.setContentsMargins(5, 2, 5, 2)
            self.ui.show_body_widget.setLayout(vLayout)

        self.ui.stackedWidget.setCurrentIndex(11)

    # 刪除模板数据
    def del_fun(self, id):
        # 查询映射信息
        request = requestUtil("/template/deleteShowTemplate/" + str(id), "DELETE")
        dataMap = request.dataMap

        A = QMessageBox.question(self.ui.template_tableWidget, '确认', '是否删除该表?', QMessageBox.Yes | QMessageBox.No)

        if A == QMessageBox.No:
            return

        if "flag" in dataMap.keys():
            QtWidgets.QMessageBox.about(share.SI.main.ui, "信息提示框", dataMap["message"]);
            self.findAll()
        else:
            QtWidgets.QMessageBox.about(share.SI.main.ui, "信息提示框", "删除显示表失败");
